﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SqlServer.Types;

namespace Storage
{
    using Exceptions;
    using EntityModels;

    #region StorageManager

    /// <summary>
    /// Rapresents the way in which the modules can handle and persist their own data.
    /// </summary>
    public sealed partial class StorageManager
    {
        #region Public methods

        #region GetAllNotificationsIdByReceiverId

        /// <summary>
        /// Gets all the notifications id for the specified user
        /// </summary>
        /// <param name="userId">The user's id.</param>
        public List<int> GetAllNotificationsIdByReceiverId(int userId)
        {
            User user = GetUserById(userId);

            var notificationsIDs = from n in Entities.Notifications
                                   where n.Receiver == user
                                   select n.Id;

            return notificationsIDs.ToList();
        }

        #endregion

        #region GetNotificationsToReadIdByUserId

        /// <summary>
        /// Gets all the unread notifications id for the specified user
        /// </summary>
        /// <param name="userId">The user's id.</param>
        public List<int> GetNotificationsToReadIdByUserId(int userId)
        {
            User user = GetUserById(userId);

            var notificationsIDs = from n in Entities.Notifications
                                   where n.Receiver == user && !n.IsRead
                                   select n.Id;

            return notificationsIDs.ToList();
        }

        #endregion

        #region GetNotificationById

        /// <summary>
        /// Gets the Notification with the specified id
        /// </summary>
        /// <param name="notificationID">The notification's id.</param>
        public Notifications GetNotificationById(int notificationID)
        {
            try
            {
                return Entities.Notifications.First(n => n.Id == notificationID);
            }
            catch (InvalidOperationException)
            {
                throw new NotificationNotFoundException(notificationID);
            }
        }

        #endregion

        #endregion
    }

    #endregion
}
